package com.tange.feature.development.toolkit;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.base.BaseActivity;
import com.tg.appcommon.android.TGToast;
import com.tange.core.backend.service.interceptor.HttpLoggingInterceptor;
import com.tange.core.backend.service.interceptor.InterceptRequest;

import java.util.ArrayList;
import java.util.List;

public class HttpInterceptListActivity extends BaseActivity {

    private ListAdapter listAdapter;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_http_request_list);
        hideActionBar();

        recyclerView = findViewById(R.id.ap_list);
        listAdapter = new ListAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(listAdapter);


    }

    @Override
    protected void onResume() {
        super.onResume();

        listAdapter.refresh(HttpLoggingInterceptor.interceptRequests);
        if (HttpLoggingInterceptor.interceptRequests.size() <= 0) {
            TGToast.showToast("暂无内容！");
        }
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected void initView() {
    }

    public class ListAdapter extends RecyclerView.Adapter<ViewHolder> {

        private List<InterceptRequest> apList = new ArrayList<>();

        public void refresh(List<InterceptRequest> list) {
            this.apList.clear();
            this.apList.addAll(list);
            notifyDataSetChanged();
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_http_intercept_list, viewGroup, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, final int position) {
            final InterceptRequest item = apList.get(position);
            viewHolder.name.setText(item.url);
            viewHolder.time.setText(item.time);

            viewHolder.itemView.setOnClickListener(v -> HttpInterceptDetailActivity.launch(v.getContext(), item.url, item.responseCode, item.params, item.responseContent, item.time));
        }

        @Override
        public int getItemCount() {
            return apList.size();
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public final TextView name;
        public final TextView time;

        public ViewHolder(View view) {
            super(view);
            name = view.findViewById(R.id.name);
            time = view.findViewById(R.id.time);
        }
    }
}